package com.apestech.framework.xml.condition.node;

import com.apestech.framework.json.JSONObject;
import com.apestech.framework.xml.condition.modul.ConditionStrategy;
import com.apestech.framework.xml.pub.core.ElementUtil;
import com.apestech.framework.xml.pub.core.NodeJson;
import org.dom4j.Element;

import java.util.List;

public class NodeStep extends ElementUtil {
    private ConditionStrategy.Step step;
    private ConditionStrategy.Step pstep;
    private Element node;
    private String type;
    private ConditionStrategy conditionStrategy;
    private JSONObject attr;

    public NodeStep(Element node, ConditionStrategy conditionStrategy, ConditionStrategy.Step pstep) throws Exception {
        this.node=node;
        this.conditionStrategy=conditionStrategy;
        this.pstep=pstep;
        type=node.getName();
        attr=getNodeAttr(node).getJSONObject("attrNode");
        if(attr==null){
            attr=new JSONObject();
        }
        attr.put("type", type);
        if(attr.get("id")==null) attr.put("id",type);
        String text=node.getTextTrim();
        if(text!=null && !"".equals(text)){
            if("sql".equals(type) || "update".equals(type)){
                attr.put("sql",text);
            } else {
                attr.put("express",text);
            }
        }
    }

    public ConditionStrategy.Step getStep() {
        return step;
    }

    public void getData() throws Exception {
        if(type.equals("while")) {
            setWhileStep();
        } else if(type.equals("if")) {
            setIfStep();
        } else if(type.equals("inputs")) {
            setInput();
        } else {
            setOther();
        }
    }

    private void addStep(){
        if(pstep==null){
            step=conditionStrategy.addStep(attr);
        } else {
            step=new ConditionStrategy.Step(attr);
        }
    }

    private void setOther() throws Exception {
        if ("sql".equals(type) || "update".equals(type)) {
            attr.put("sql", node.getTextTrim());
        }
        if (attr.get("id") == null) attr.put("id", node.getName());
        String text = node.getTextTrim();
        if (text != null && !"".equals(text) && !"sql".equals(type) && !"update".equals(type))
            attr.put("express", text);
        addStep();
        //conditionStrategy.addField(attr);
        //getFieldList(node, null, null, false);
    }

    private void setIfStep() throws Exception {
        ConditionStrategy.If fif=new ConditionStrategy.If(attr);
        addStep();
        getIf(node, fif);
        step.setFif(fif);
    }

    private void setWhileStep() throws Exception {
        ConditionStrategy.While cwhile = new ConditionStrategy.While(attr);
        addStep();
        getWhile(node, cwhile);
        step.setFwhile(cwhile);
    }

    private void setInput() throws Exception {
        NodeJson json=new NodeJson(node);
        json.expain();
        conditionStrategy.setMust_input(json.getResult());
    }

    private void getIf(Element e, ConditionStrategy.If fif) throws Exception {
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element ele=(Element) list.get(i);
                String type=ele.getName();
                JSONObject attr=getNodeAttr(ele).getJSONObject("attrNode");
                if(attr==null) attr=new JSONObject();
                attr.put("type", type);
                if("exp".equals(type) ){
                    fif.setExp(ele.getTextTrim());
                } else if("then".equals(type) ){
                    ConditionStrategy.Then fthen=new ConditionStrategy.Then(attr);
                    fif.setFthen(fthen);
                    getThen(ele, fif, false);
                } else if("else".equals(type)){
                    fif.setbElse(true);
                    ConditionStrategy.Then fthen=new ConditionStrategy.Then(attr);
                    fif.setEthen(fthen);
                    getThen(ele, fif, true);
                }
            }
        }
    }

    private void getThen(Element e, ConditionStrategy.If fif, boolean belse) throws Exception {
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element ele = (Element) list.get(i);
                NodeStep nodeStep=new NodeStep(ele, conditionStrategy, step);
                nodeStep.getData();
                if(belse){
                    fif.getEthen().addStep(nodeStep.getStep());
                } else {
                    fif.getFthen().addStep(nodeStep.getStep());
                }
            }
        }
    }

    private void getWhile(Element e, ConditionStrategy.While cwhile) throws Exception {
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element ele = (Element) list.get(i);
                String type=ele.getName();
                JSONObject attr=getNodeAttr(ele).getJSONObject("attrNode");
                if(attr==null) attr=new JSONObject();
                attr.put("type", type);
                if("sql".equals(type) || "queryId".equals(type) || "json".equals(type)|| "xls".equals(type) || "array".equals(type)){
                    if("sql".equals(type)){
                        attr.put("sql", ele.getTextTrim());
                    }
                    cwhile.setSql(new ConditionStrategy.Step(attr));
                } else if("result".equals(type) ){
                    cwhile.setReslut(attr);
                    setFieldlist(ele, cwhile);
                }
            }
        }
    }

    private void setFieldlist(Element e, ConditionStrategy.While cwhile) throws Exception {
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element ele = (Element) list.get(i);
                NodeStep nodeStep=new NodeStep(ele, conditionStrategy, step);
                nodeStep.getData();
                cwhile.addStep(nodeStep.getStep());
            }
        }
    }


}
